//
//  WYMainViewController.swift
//  Weibo
//
//  Created by Mr.Run on 2016/11/19.
//  Copyright © 2016年 Mr.Run. All rights reserved.
//

import UIKit

class WYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildControllers()
        setupComposeBtn()
    }
    
    lazy var compsoeBtn: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    @objc fileprivate func composeBtnStatus() {
        print(#function)
    }
    
}

extension WYMainViewController{
    
    func setupComposeBtn() {
        tabBar.addSubview(compsoeBtn)
        
        let count = CGFloat(childViewControllers.count)
        let  w = (tabBar.bounds.width / count) - 1.0
        
        compsoeBtn.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        compsoeBtn.addTarget(self, action:#selector(composeBtnStatus), for: .touchUpInside)
        
    }
    
    func setupChildControllers() {
        
        let array = [
        
            ["clsName": "WYHomeViewController", "title": "首页", "imageName": "home"],
            ["clsName": "WYMessageViewController", "title": "消息", "imageName": "message_center"],
            ["clsName": "UIViewController"],
            ["clsName": "WYDiscoverViewController", "title": "发现", "imageName": "discover"],
            ["clsName": "WYProfileViewController", "title": "我", "imageName": "profile"],
        
        ]
        var arrM = [UIViewController]()
        for dict in array {
            arrM.append(contrlller(dict: dict))
        }
        viewControllers = arrM
        
    }
    
    private func contrlller(dict: [String : String]) -> UIViewController{
        guard let clsName = dict["clsName"],
                    let title = dict["title"],
                    let imageName = dict["imageName"],
                    let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type else {
                        return UIViewController()
        }
        let vc = cls.init()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_highlighted")?.withRenderingMode(.alwaysOriginal)
//        vc.tabBarItem.setBadgeTextAttributes([String : Any]?, for: <#T##UIControlState#>)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted)
//        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontSize], for: UIControlState(rawValue: 0))
        let nav = WYNavigationController(rootViewController: vc)
        return nav
        
    }
    
}

